# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# application.py
# written by: 
#     Jan Jokela <jan@devion.org>

__version__ = 0.7
__doc__ = 'fluid application'

import pygame
from pygame.locals import *
import qgl
import qgle
from fluid.eye import *
import fluid.events as events
import fluid.actions as actions
import base.config.config as config

class Application:
    
    def __init__(self):
        if config.get_option('app.graphics', 'fullscreen', 'boolean'):
            flags = OPENGL|DOUBLEBUF|HWSURFACE|FULLSCREEN
        elif not config.get_option('app.graphics', 'fullscreen', 'boolean'):
            flags = RESIZABLE|OPENGL|DOUBLEBUF|HWSURFACE
        resolution = (config.get_option('app.graphics', 'xres', 'int'), config.get_option('app.graphics', 'yres', 'int')) 
        screen_dimensions = (0, 0, config.get_option('app.graphics', 'xdim', 'int'), config.get_option('app.graphics', 'ydim', 'int')) 
        aspect = (config.get_option('app.graphics', 'xaspect', 'float') / config.get_option('app.graphics', 'yaspect', 'float')) 
        config_screen(resolution, flags=flags, caption='Devion mediaplex')
        config_viewport(screen_dimensions, aspect)
        pygame.mouse.set_visible(False)
        
        run = config.get_option('app', 'run', 'int')
        if run == 1:
            import base.intro as intro
            self.intro = intro.Intro()
        else:
            import base.menu as menu
            menu.MainMenu()
        config.set_option('app', 'run', str(run+1))
        root_node.accept(compiler)
        clock = pygame.time.Clock()
        
        target_fps = config.get_option('app.graphics', 'fps', 'int')
        fps = config.get_option('app.graphics', 'fps', 'int')
        
        while True:
            eventhandler.watch()
            actionhandler.watch()
            
            root_node.accept(compiler)
            
            root_node.accept(render)
            #qgle.drive(root_node, clock.tick(30) / 1000.0)
            qgle.drive(root_node)
            clock.tick(target_fps)
            fps = clock.get_fps()
            #print fps
            pygame.display.flip()
    
Application()
